﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Hellquest
{
    public class Transform : Component
    {
        private Vector2 lastPosition;
        public Vector2 LocalPosition;

        public Vector2 WorldPosition
        {
            get
            {
                if (gameObject.Parent != null)
                    return LocalPosition + gameObject.Parent.transform.WorldPosition;
                else
                    return LocalPosition;
            }

            set
            {
                if (gameObject.Parent != null)
                    LocalPosition = value - gameObject.Parent.transform.WorldPosition;
                else
                    LocalPosition = value;
            }
        }
        public Vector2 PositionDiff { get { return Position - lastPosition; } }

        private float lastRotation;
        private float mRotation;
        public float Rotation
        {
            get { return mRotation; }
            set { mRotation = value; }
        }

        public bool RotationChanged { get { return lastRotation != Rotation; } }
        public bool PositionChanged { get { return lastPosition != Position; } }

        public Transform()
        {
        }

        public Transform(Vector2 _position)
        {
            LocalPosition = _position;
            lastPosition = _position;
            Rotation = 0;
            lastRotation = 0;
        }

        public override void OnAdd(GameObject _gameObject)
        {
            base.OnAdd(_gameObject);
            gameObject.transform = this;
        }

        public override void OnRemove()
        {
            gameObject.transform = null;
            base.OnRemove();
        }

        public override void LateUpdate(GameTime _gameTime)
        {
            base.LateUpdate(_gameTime);
            lastPosition = Position;
            lastRotation = Rotation;
        }
    }
}
